﻿using CIS.Web.Common;
using CIS.Web.Models;
using CIS.Web.Properties;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace CIS.Web.Controllers
{
	[RoutePrefix("ConferenceHome")]
	public class ConferenceHomeController : BaseDbController
	{
		public ActionResult Index()
		{
			string controller = RouteData.Values["controller"].ToString();
			string actionName = RouteData.Values["action"].ToString();
			Utility.InsertLog(LoginUser, Request.Url.ToString(), controller, actionName);

			return View();
		}

		public async Task<JsonCamelCaseResult> GetData(int? page, int? rows, string sort, string order, ConferenceMasterModel model)
		{
			int take = rows ?? ConstValues.DefaultGridRows;
			int skip = ((page ?? 1) - 1) * take;

			var sql = DbContext.ConferenceMaster.AsNoTracking().Where(x => !string.IsNullOrEmpty(x.Status));

			if (!LoginUser.IsSSA)
			{
				var conferenceIDList = await DbContext.ViewConferenceRoleAction.AsNoTracking()
						.Where(x => x.UserName == LoginUser.UserName)
						.Select(x => x.ConferenceID)
						.ToListAsync();

				sql = sql.Where(x => conferenceIDList.Contains(x.ID) || x.Creator == LoginUser.UserName);
			}

			int total = await sql.CountAsync();

			if (!string.IsNullOrWhiteSpace(model.ConferenceName))
			{
				sql = sql.Where(x => x.ConferenceName.Contains(model.ConferenceName) || x.ConferenceNickName.Contains(model.ConferenceName));
			}

			if (!string.IsNullOrEmpty(sort))
			{
				sql = sql.OrderBy(sort + ConstValues.chrSpace + order).Skip(skip).Take(take);
			}
			else
			{
				sql = sql.OrderByDescending(x =>x.ConferenceStartDate).ThenBy(x => x.Sort).ThenByDescending(x => x.UpdateDate).ThenBy(x => x.ConferenceName).Skip(skip).Take(take);
			}

			var sqlResult = await sql.ToListAsync();

			var result = sqlResult.Select(x => new
			{
				Id = x.ID,
				ConferenceName = x.ConferenceName,
				OperateTime = x.OperateTime,
				ConferenceStartDate = Request.Cookies["_culture"].Value == ConstValues.Language.Cn ?
						x.ConferenceStartDate.Value.ToString("yyyy-MM-dd") :
						x.ConferenceStartDate.Value.ToString("MM/dd/yyyy"),
				ConferenceEndDate = Request.Cookies["_culture"].Value == ConstValues.Language.Cn ?
						x.ConferenceEndDate.Value.ToString("yyyy-MM-dd") :
						x.ConferenceEndDate.Value.ToString("MM/dd/yyyy"),
				OperateLocation = x.OperateLocation,
				HomePageUrl = x.HomePageUrl,
				CreateTime = Request.Cookies["_culture"].Value == ConstValues.Language.Cn ?
						x.CreateDate.Value.ToString("yyyy-MM-dd HH:mm") :
						x.CreateDate.Value.ToString("MM/dd/yyyy HH:mm"),
				Status = x.Status,
				ConferenceNickName = x.ConferenceNickName,
				IsInHome = x.IsInHome ? Resources.Yes : Resources.No,
				IsRecommend = x.IsRecommend ? Resources.Yes : Resources.No,
				IsDisplay = x.IsDisplay ? Resources.Yes : Resources.No,
				IsDisplayMobile = x.IsDisplayMobile ? Resources.Yes : Resources.No,
				IsDisplayMobileHome = x.IsDisplayMobileHome ? Resources.Yes : Resources.No,
			});

			return JsonCamelCase(new
			{
				Total = total,
				Rows = result,
			},
			JsonRequestBehavior.AllowGet);
		}

		public JsonCamelCaseResult HasConferenceRoleBoolean(string id)
		{
			var result = (User as ICISPrincipal).IsSSA || (User as ICISPrincipal).CSAList.Contains(id) ||
					Utility.HasConferenceRole((User as ICISPrincipal), id, ConstValues.FunctionId.ConferenceInfo, RoleType.浏览);

			if (result)
			{
				return JsonCamelCase(Utility.CreateSuccessJsonResult(ConstValues.AjaxSuccessResult, result),
						JsonRequestBehavior.AllowGet);
			}
			else
			{
				return JsonCamelCase(Utility.CreateFailJsonResult(Resources.MsgNoEntryRole),
						JsonRequestBehavior.AllowGet);
			}
		}
	}
}